﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;

public partial class NewMasterPage : MasterBase
{
    MyDMSEntity _myDmsEntity=new MyDMSEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (_author != null)
            {
                foreach (Department dept in _myDmsEntity.Department)
                {
                    bool _iscontained2 = false;
                    TreeNode deptTn = new TreeNode(dept.Department_Name, dept.Department_Code.ToString());

                    if (_author.department_Code == dept.Department_Code)
                    {
                        _iscontained2 = true;
                    }
                    foreach (Project proj in _myDmsEntity.Project.Where(p => p.Department_Code == dept.Department_Code))
                    {
                        #region 控制是否显示 意义不大

                        bool _iscontained1 = false;
                        if (proj.Is_Anonymous)
                        {
                            _iscontained1 = true;
                            _iscontained2 = true;
                        }
                        if (_author.department_Code == dept.Department_Code)
                        {
                            _iscontained1 = true;
                            _iscontained2 = true;
                        }
                        if ((!proj.Is_Anonymous) && (_author.department_Code != dept.Department_Code))
                        {
                            _iscontained1 = false;
                        }

                        #endregion

                        if (_iscontained1)
                        {
                            TreeNode projTn = new TreeNode(proj.Project_Title,
                                                           dept.Department_Code.ToString() + '|' +
                                                           proj.Project_Code.ToString());
                            foreach (Task task in _myDmsEntity.Task.Where(t => t.Project_Code == proj.Project_Code))
                            {
                                TreeNode taskTn = new TreeNode(task.Task_Title,
                                                               dept.Department_Code.ToString() + '|' +
                                                               proj.Project_Code.ToString() + '|' +
                                                               task.Task_Code.ToString());
                                foreach (
                                    Folder folder in
                                        _myDmsEntity.Folder.Where(df => df.Task_Code == task.Task_Code))
                                {
                                    TreeNode folderTn = new TreeNode(folder.Folder_Title,
                                                                     dept.Department_Code.ToString() + '|' +
                                                                     proj.Project_Code.ToString() + '|' +
                                                                     task.Task_Code.ToString() + '|' +
                                                                     folder.Folder_Code);
                                    taskTn.ChildNodes.Add(folderTn);
                                }
                                projTn.ChildNodes.Add(taskTn);
                            }
                            deptTn.ChildNodes.Add(projTn);
                        }
                    }
                    if (_iscontained2)
                    {
                        TreeView1.Nodes.Add(deptTn);
                    }
                }
            }
            TreeNodesExpandStateManager.RestoreExpandStateToTreeFromSession(TreeView1);
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView tree = sender as TreeView;
        if ((bool) (tree.SelectedNode.Expanded.HasValue?tree.SelectedNode.Expanded:false))
        {
            tree.SelectedNode.Collapse();
        }
        else
        {
            tree.SelectedNode.Expand();
        }
        TreeNodesExpandStateManager.SaveExpandStateInSession(tree);
        var result = tree.SelectedNode.Value.Split('|');
        //是Department节点
        if (result.Count() == 1)
        {
            //科内人员会看到页面
            if (_author.department_Code == int.Parse(result[0]))
            {
                //科长进行管理 管理科内的中项 查看科内最新文件
                //科员只能浏览科内的最新文件
                Response.Redirect(string.Format("./Department.aspx?dept={0}",int.Parse(result[0])));
            }
        }
        //是Project节点 
        if (result.Count() == 2)
        {
            //科内人员可以浏览proj的最新文件
            Response.Redirect(string.Format("./Project.aspx?dept={0}&proj={1}", int.Parse(result[0]), int.Parse(result[1])));
        }
        //Task节点
        if (result.Count() == 3)
        {
            //科内人员可以Task内的所有文件
            Response.Redirect(string.Format("./Task.aspx?dept={0}&proj={1}&task={2}", int.Parse(result[0]), int.Parse(result[1]), int.Parse(result[2])));
        }
        //Folder节点
        if (result.Count() == 4)
        {
            //浏览Task中某个Folder的所有文件
            Response.Redirect(string.Format("./Folder.aspx?dept={0}&proj={1}&task={2}&folder={3}", int.Parse(result[0]), int.Parse(result[1]), int.Parse(result[2]),int.Parse(result[3])));
        }
    }
}
